package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.ReserveMapper;
import com.hqyj.dao.SysMenuMapper;
import com.hqyj.dao.TableMenuMapper;
import com.hqyj.pojo.SysMenu;
import com.hqyj.pojo.TableMenu;
import com.hqyj.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private SysMenuMapper dao;

    @Autowired
    private TableMenuMapper tableMenuMapper;

    @Autowired
    private ReserveMapper reserveMapper;

    public HashMap<String, Object> addMenu(SysMenu menu) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int n = dao.insertSelective(menu);
        if (n > 0) {
            map.put("info", "添加成功");
        } else {
            map.put("info", "添加失败");
        }
        return map;
    }

    public HashMap<String, Object> menuList(SysMenu menu) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //分页
        PageHelper.startPage(menu.getPage(), menu.getRow());

        List<SysMenu> menuList = dao.selectByType(menu);

        //3 获取分页对象
        PageInfo<SysMenu> pageInfo = new PageInfo<SysMenu>(menuList);

        if (menuList.size() > 0) {

            //获取当前页的集合
            map.put("info", pageInfo.getList());
            //总条数
            Long total = pageInfo.getTotal();
            //总页数
            int totalPage = pageInfo.getPages();
            map.put("total", total);
            map.put("totalPage", totalPage);
        } else {
            map.put("info", "暂无商品");
        }
        return map;
    }

    public HashMap<String, Object> menuListAll(SysMenu menu) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (menu.geteName() != null) {
            menu.seteName("%" + menu.geteName() + "%");
        }
        List<SysMenu> menuList = dao.menuList(menu);

        if (menuList.size() > 0) {
            map.put("info", menuList);
        } else {
            map.put("info", "暂无商品");
        }
        return map;
    }

    //点菜
    public HashMap<String, Object> order(SysMenu menu) {
        System.out.println(menu);
        HashMap<String, Object> map = new HashMap<String, Object>();
        TableMenu tableMenu = new TableMenu();
        SysMenu sysMenu = dao.selectByPrimaryKey(menu.geteId());
        System.out.println(sysMenu);
        if (menu.getNumber() <= 0) {
            map.put("info", "选择的份数不能小于等于0");
        } else {
            int n = reserveMapper.selectIdByUserAndState(menu.getUserId());
            System.out.println(n);
            if (n > 0) {
                tableMenu.seteId(sysMenu.geteId());
                tableMenu.settNumber(menu.getNumber());
                tableMenu.seteName(sysMenu.geteName());
                tableMenu.settEPrice(sysMenu.getePrice());
                tableMenu.settECost(sysMenu.geteCost());
                tableMenu.settEState("待上菜");
                tableMenu.settECheck("未付款");
                tableMenu.settId(n);
            }

            int i = tableMenuMapper.insertSelective(tableMenu);
            if (i > 0) {
                map.put("info", "点餐成功");
            }

        }
        return map;
    }

    public HashMap<String, Object> bitDel(String arrId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //
        if (arrId != null) {
            String ids[] = arrId.split(",");
            if (ids.length == 1) {
                List<String> list = Arrays.asList(ids);
                int num = dao.delBitch(list);
                if (num > 0) {
                    map.put("info", "批量删除成功");
                } else {
                    map.put("info", "批量删除失败");
                }
            } else {
                //接收数组
                String id[] = new String[ids.length - 1];
                for (int i = 1; i < ids.length; i++) {
                    id[i - 1] = ids[i];
                }
                //数组转集合
                List<String> list = Arrays.asList(id);
                int num = dao.delBitch(list);
                if (num > 0) {
                    map.put("info", "批量删除成功");
                } else {
                    map.put("info", "批量删除失败");
                }
            }

        }
        return map;
    }
}
